package user.dao.impl;

import user.dao.AdministratorDao;
import user.dao.BaseDao;
import user.entity.Administrator;

import java.sql.ResultSet;
import java.sql.SQLException;

public class AdministratorDaoImpl extends BaseDao implements AdministratorDao {
    @Override
    public Administrator Login(String Account, String cipher) {
        String sql="select * from Administrator where Account=? and cipher=?";
        ResultSet rs=this.executeQuery(sql,Account,cipher);
        Administrator administrator=null;
        try {
            while (rs.next()){
                administrator=new Administrator();
                administrator.setId(rs.getInt(1));
                administrator.setAdminName(rs.getString(2));
                administrator.setAccount(rs.getString(3));
                administrator.setCipher(rs.getString(4));
                administrator.setTelephone(rs.getString(5));
                administrator.setEmail(rs.getString(6));
                administrator.setAuthentication(rs.getInt(7));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            this.closeConnection(null,null,rs);
        }
        return administrator;
    }

    @Override
    public int AddInfo(Administrator a) {
        String sql="INSERT INTO Administrator(`AdminName`, `Account`, `cipher`, `Telephone`, `Email`, `authentication`,`balance`) VALUES(?,?,?,?,?,?,?)";
        return this.executeUpdate(sql,a.getAdminName(),a.getAccount(),a.getCipher(),a.getTelephone(),a.getEmail(),a.getAuthentication(),0.00);
    }

    @Override
    public int UpdateInfo(Administrator a) {
        String sql="UPDATE `BurgerKingDB`.`Administrator` SET `cipher` =?  WHERE `Email` =?";
        return this.executeUpdate(sql,a.getCipher(),a.getEmail());
    }

    @Override
    public int UpdatePhone(Administrator a) {
        String sql="UPDATE `BurgerKingDB`.`Administrator` SET `cipher` =?  WHERE `Telephone` =?";
        return this.executeUpdate(sql,a.getCipher(),a.getTelephone());
    }
}
